package com.cskaoyan.controller.wxcontroller;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author Gan
 * @Date 2021/8/5 20:10
 * @Description:
 */
public class MyTest {

    public static void main(String[] args) {
        Account account = new Account();
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        for (int i = 0; i < 100; i++) {
            executorService.execute(new AddMoney(account, 1));
        }
        executorService.shutdown();

    }

    /**
     * 银行账户类 存款
     */
    static class Account {
        //账户余额
        private double balance;

        public void deposit(double money) {
            double newBalance = balance + money;
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                System.out.println("模拟此业务~花费一段时间");
                e.printStackTrace();
            }
            balance = newBalance;
        }

        //获得银行账户余额
        public double getBalance() {
            return balance;
        }
    }

    /**
     * 加钱操作
     */
    static class AddMoney implements Runnable {

        private Account account;

        private double money;

        public AddMoney(Account account, double money) {
            this.account = account;
            this.money = money;
        }

        @Override
        public void run() {
            account.deposit(money);
        }
    }

    /**
     * 二分查找
     * @param nums
     * @param target
     * @return
     */
    private int binarySearch(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return nums[mid];
            } else if (target < nums[mid]) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }

    //选择排序
    private static void selectSort(int[] nums) {
        for (int i = 0; i < nums.length; ++i) {
            int minIndex = i;
            for (int j = i + 1; j < nums.length; ++j) {
                if (nums[minIndex] > nums[j]) {
                    minIndex = j;
                }
            }
            swap(nums, i, minIndex);
        }
    }

    private static void swap(int[] nums, int i, int minIndex) {
        int temp = nums[i];
        nums[i] = nums[minIndex];
        nums[minIndex] = temp;
    }

    /**
     * 快排
     * @param arr
     * @param low  = 0
     * @param high = arr.length - 1
     */
    public static void quickSort(int[] arr, int low, int high) {

        int pivot = arr[low];
        int i = low;
        int j = high;
        while (i < j) {
            while ((i < j) && pivot < arr[j]) {
                j--;
            }
            while ((i < j) && arr[i] < pivot) {
                i++;
            }
            if ((i < j) && (arr[i] == arr[j])) {
                i++;
            } else {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
        if (i - 1 > low)
            quickSort(arr, low, i - 1);
        if (j + 1 < high)
            quickSort(arr, j + 1, high);
    }


}



